using System;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace SharePointAnalyzer
{
    public class Analyzer   
    {
        public Analyzer(XmlWriter xmlWriter, Hashtable tableLists, Hashtable tableWebs)
        {
            Writer = xmlWriter;
            htLists = tableLists;
            htWebs = tableWebs;
        }
        private Hashtable htLists;
        private Hashtable htWebs;
        private XmlWriter writer;

        public XmlWriter Writer
        {
            get
            {
                return writer;
            }
            set
            {
                writer = value;
            }
        }

        public void ProcessWeb(SPWeb web)
        {
            writer.WriteStartElement("web");
            writer.WriteElementString("title", web.Title);
            writer.WriteElementString("template", web.WebTemplate);
            writer.WriteElementString("templateid", web.WebTemplateId.ToString());
            if (htWebs.ContainsKey(web.WebTemplate))
            {
                WebInfo wi = (WebInfo)htWebs[web.WebTemplate];
                
                wi.instanceCount++;
            }
            else
            {
                WebInfo wi = new WebInfo();
                wi.instanceCount++;
                htWebs.Add(web.WebTemplate, (object)wi);
            }
            try
            {
                writer.WriteElementString("lastitemmodifieddate", web.LastItemModifiedDate.ToShortDateString());
            }
            catch(Exception TimeEx)
            {
                writer.WriteElementString("lastitemmodifieddate", web.Site.LastContentModifiedDate.ToShortDateString());

            }

            writer.WriteStartElement("lists");
            foreach (SPList list in web.Lists)
            {
                
                writer.WriteStartElement("list");
                writer.WriteElementString("title", list.Title);
                writer.WriteElementString("template", list.BaseTemplate.ToString());
                if(htLists.ContainsKey(list.BaseTemplate.ToString()))
                {
                    ListInfo li=(ListInfo)htLists[list.BaseTemplate.ToString()];
                    li.itemCount += list.ItemCount;
                    li.instanceCount++;
                }
                else
                {
                    ListInfo li = new ListInfo();
                    li.listName = list.Title;
                    li.itemCount = list.ItemCount;
                    li.instanceCount++;
                    htLists.Add(list.BaseTemplate.ToString(), (object)li);
                }
                string schema = list.SchemaXml;
                int pos = schema.IndexOf("ServerTemplate=\"");
                if (pos > 0)
                    schema = schema.Substring(pos + 16);
                pos = schema.IndexOf("\"");
                if (pos > 0)
                    schema = schema.Remove(pos);
                writer.WriteElementString("listtemplateid", schema);
                writer.WriteElementString("lastitemmodifieddate", list.LastItemModifiedDate.ToShortDateString());
                writer.WriteElementString("itemcount", list.ItemCount.ToString());

                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteStartElement("webs");
            foreach (SPWeb subWeb in web.GetSubwebsForCurrentUser())
            {
                
                ProcessWeb(subWeb);
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            
        }
    }
}
